﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblAuditTrailCatDAL:ImtblAuditTrailCat
    {
        HKEA_CMSEntities model;
        mtblAuditTrailCat trailcat;
        public mtblAuditTrailCatDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblAuditTrailCatDAL GetInstance()
        {      
            return new mtblAuditTrailCatDAL();
        }

        public int InsertENT(mtblAuditTrailCat ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblAuditTrailCats.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblAuditTrailCat> GetListByENT(mtblAuditTrailCat ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblAuditTrailCats.Where(a => a.AuditTrailCatUID > 0);
            if (ent.AuditTrailCatUID > 0)
                result = result.Where(a => a.AuditTrailCatUID == ent.AuditTrailCatUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblAuditTrailCat> GetListByENT(mtblAuditTrailCat ent)
        {
            var result = model.mtblAuditTrailCats.Where(a => a.AuditTrailCatUID > 0);
            if (ent.AuditTrailCatUID > 0)
                result = result.Where(a => a.AuditTrailCatUID == ent.AuditTrailCatUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(mtblAuditTrailCat ent)
        {
            var result = model.mtblAuditTrailCats.Where(a => a.AuditTrailCatUID > 0);
            if (ent.AuditTrailCatUID > 0)
                result = result.Where(a => a.AuditTrailCatUID == ent.AuditTrailCatUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblAuditTrailCat ent)
        {
            trailcat = model.mtblAuditTrailCats.Where(a => a.AuditTrailCatUID == ent.AuditTrailCatUID).FirstOrDefault();
            if (trailcat != null)
            {
                trailcat.AuditTrailCat_Chi = ent.AuditTrailCat_Chi;
                trailcat.AuditTrailCat_Eng = ent.AuditTrailCat_Eng;
                trailcat.Deleted = ent.Deleted;
                trailcat.LastModifyBy = ent.LastModifyBy;
                trailcat.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblAuditTrailCat ent)
        {
            trailcat = model.mtblAuditTrailCats.Where(a => a.AuditTrailCatUID == ent.AuditTrailCatUID).FirstOrDefault();
            if (trailcat != null)
            {
                model.mtblAuditTrailCats.DeleteObject(trailcat);
            }
            return model.SaveChanges();
        }
    }
}
